NVM.GoToTop = (function () {
  var __core = NOVEM.Core;

  var _init = function (element) {
    var elSpeed = element.getAttribute("data-speed") || 700,
      elEasing = element.getAttribute("data-easing");

    element.onclick = function (e) {
      __core.scrollTo(0, Number(elSpeed), elEasing);
      e.preventDefault();
    };
  };

  var _scroll = function (element) {
    var body = __core.getVars.elBody.classList;

    var elMobile = element.getAttribute("data-mobile") || "false",
      elOffset = element.getAttribute("data-offset") || 450;

    if (
      elMobile == "false" &&
      (body.contains("device-xs") ||
        body.contains("device-sm") ||
        body.contains("device-md"))
    ) {
      return true;
    }
    element.querySelector(".progress-square path").style.strokeDashoffset =
      element.querySelector(".progress-square path").getTotalLength() -
      ((document.body.scrollTop || document.documentElement.scrollTop) *
        element.querySelector(".progress-square path").getTotalLength()) /
        (document.documentElement.scrollHeight -
          document.documentElement.clientHeight);
    (document.body.scrollTop || document.documentElement.scrollTop) >= elOffset
      ? element.classList.add("active-progress")
      : element.classList.remove("active-progress");

    if (window.scrollY > Number(elOffset)) {
      body.add("gototop-active");
    } else {
      body.remove("gototop-active");
    }
  };

  return {
    init: function (selector) {
      selector = __core.getSelector(selector, false);
      if (selector.length < 1) {
        return true;
      }

      _init(selector[0]);
      _scroll(selector[0]);

      selector[0].querySelector("svg path").style.transition =
        selector[0].querySelector("svg path").style.WebkitTransition = "none";
      selector[0].querySelector("svg path").style.strokeDasharray =
        selector[0].querySelector("svg path").getTotalLength() +
        " " +
        selector[0].querySelector("svg path").getTotalLength();
      selector[0].querySelector("svg path").style.strokeDashoffset = selector[0]
        .querySelector("svg path")
        .getTotalLength();
      selector[0].querySelector("svg path").getBoundingClientRect();
      selector[0].querySelector("svg path").style.transition =
        selector[0].querySelector("svg path").style.WebkitTransition =
          "stroke-dashoffset 10 linear";

      window.addEventListener(
        "scroll",
        function () {
          _scroll(selector[0]);
        },
        { passive: true }
      );
    },
  };
})();
